package leja.notis;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void btnOpenClicked(View view) {
        Toast.makeText(getBaseContext(),
                "Coming soon",
                Toast.LENGTH_SHORT).show();
    }

    public void btnSaveClicked(View view) {
        EditText editText = (EditText) findViewById(R.id.input_text);
        String text = editText.getText().toString();
        String fileName = "mysdfile.txt"; // TO DO: create filename generator
        SaveOnExternal(text, fileName);
    }

    public void btnSaveIntClicked(View view) {
        EditText editText = (EditText) findViewById(R.id.input_text);
        String text = editText.getText().toString();
        String fileName = "mysdfile.txt"; // TO DO: create filename generator
        SaveOnInternal(text, fileName);
    }

    public void SaveOnExternal(String text, String fileName) {
        if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            Toast.makeText(this, R.string.err_no_sd, Toast.LENGTH_SHORT).show();
            return;
        }

        String path = Environment.getExternalStorageDirectory().getPath() + "/" + fileName;
        try {
            File file = new File(path);
            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            osw.append(text);
            osw.close();
            fOut.close();

            Toast.makeText(getBaseContext(),
                    R.string.save_success,
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void SaveOnInternal(String text, String fileName) {
        try {
            FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
            fos.write(text.getBytes());
            fos.close();

            Toast.makeText(getBaseContext(),
                    R.string.save_success,
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }

}
